import { IKey } from "../common/Common";

/**
 * 红点事件/委托
 */
export interface BadgeAction {
    caller?: any;
    ages?: any[];

    /**数字红点,大于 0 代表有红点 */
    method?(ages?: any): number;
}

export class BadgeNode {

    /**此节点属于哪些组 */
    private _groups = new Set<IKey>();

    /**节点值 */
    value: number = 0;

    /**节点内容 */
    key: IKey;

    /**节点委托方法 */
    action: BadgeAction;

    addGropu(group: IKey) {
        this._groups.add(group);
    }

    update() {
        let oldV = this.value;
        this.value = this.actionConst;

        return oldV != this.value;
    }

    get groups(): IKey[] {
        return Array.from(this._groups);
    }

    get groupSize() {
        return this._groups.size;
    }

    get flag() {
        return this.value != null && this.value > 0;
    }

    get actionConst(): number {
        if (!this.action) {
            return 0;
        }

        return this.action.method.apply(this.action.caller, this.action.ages);
    }
}
